package collection;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;




public class StreamApiDemo2 {
    public static void main(String[] args) {
        //生成一个随机数的Integer泛型的集合，取值范围0~99
        Random rand = new Random();
        List<Integer> list = new ArrayList<Integer>();
        for (int i=0; i<10; i++) {
            list.add(rand.nextInt(100));
        }
        System.out.println("List: " + list);

        //通过流式操作取出最小值
        Integer value = list.stream().min((e1, e2) -> e1 - e2).get();    //终端环节.get()为取值
        System.out.println("Min: " + value);

        //通过流式操作取出最大值
        value = list.stream().max((e1, e2) -> e1 - e2).get();
        System.out.println("Max: " + value);

        //取出所有元素的求和
        value = list.stream().reduce((e1, e2) -> e1 + e2).get();
        System.out.println("Sum: " + value);

    }
}
